const app = getApp()

Page({
  data: {
    location: '上海',
    currentTab: 'home',
    hotTags: [
      { id: 1, name: '高血压' },
      { id: 2, name: '冠心病' },
      { id: 3, name: '如何控制血糖' }
    ],
    mainServices: [
      {
        id: 1,
        name: '家政护理',
        description: '快速上门服务',
        icon: '/static/zhyl/服务客户.png',
        iconColor: '#9D7DFF',
        iconBg: 'rgba(157, 125, 255, 0.1)',
        path: '/pages/services/homecare/index'
      },
      {
        id: 2,
        name: '康复理疗',
        description: '全套护理方案',
        icon: '/static/zhyl/康复理疗科.png',
        iconColor: '#FF7D7D',
        iconBg: 'rgba(255, 125, 125, 0.1)',
        path: '/pages/services/rehabilitation/index'
      },
      {
        id: 3,
        name: '上门体检',
        description: '专业医师团队',
        icon: '/static/zhyl/icon.png',
        iconColor: '#7D9DFF',
        iconBg: 'rgba(125, 157, 255, 0.1)',
        path: '/pages/services/examination/index'
      }
    ],
    functionItems: [
      {
        id: 'healthData',
        name: '健康数据',
        icon: '/static/zhyl/数据.png',
        iconColor: '#4CAF50',
        iconBg: 'rgba(76, 175, 80, 0.1)',
        path: '/pages/health/data/index'
      },
      {
        id: 'deviceCenter',
        name: '设备中心',
        icon: '/static/zhyl/设备.png',
        iconColor: '#7D9DFF',
        iconBg: 'rgba(125, 157, 255, 0.1)',
        path: '/pages/device/center/index'
      },
      {
        id: 'medicineInfo',
        name: '用药信息',
        icon: '/static/zhyl/药瓶.png',
        iconColor: '#FFC107',
        iconBg: 'rgba(255, 193, 7, 0.1)',
        path: '/pages/medicine/info/index'
      },
      {
        id: 'healthyDiet',
        name: '健康膳食',
        icon: '/static/zhyl/2_5膳食健康.png',
        iconColor: '#2196F3',
        iconBg: 'rgba(33, 150, 243, 0.1)',
        path: '/pages/diet/index'
      },
      {
        id: 'diseaseLibrary',
        name: '疾病宝典',
        icon: '/static/zhyl/icon.png',
        iconColor: '#FF5722',
        iconBg: 'rgba(255, 87, 34, 0.1)',
        path: '/pages/disease/library/index'
      },
      {
        id: 'healthArchive',
        name: '健康档案',
        icon: '/static/zhyl/健康档案.png',
        iconColor: '#4CAF50',
        iconBg: 'rgba(76, 175, 80, 0.1)',
        path: '/pages/health/archive/index'
      },
      {
        id: 'elderlyActivity',
        name: '老年活动',
        icon: '/static/zhyl/老年活动.png',
        iconColor: '#9C27B0',
        iconBg: 'rgba(156, 39, 176, 0.1)',
        path: '/pages/elderly/activity/index'
      },
      {
        id: 'healthNews',
        name: '健康资讯',
        icon: '/static/zhyl/健康咨询.png',
        iconColor: '#FFC107',
        iconBg: 'rgba(255, 193, 7, 0.1)',
        path: '/pages/health/news/index'
      },
      {
        id: 'elderlyInstitutions',
        name: '养老机构',
        icon: '/static/zhyl/养老机构-copy.png',
        iconColor: '#3F51B5',
        iconBg: 'rgba(63, 81, 181, 0.1)',
        path: '/pages/elderly/institutions/index'
      },
      {
        id: 'healthLecture',
        name: '健康讲堂',
        icon: '/static/zhyl/健康讲堂.png',
        iconColor: '#F44336',
        iconBg: 'rgba(244, 67, 54, 0.1)',
        path: '/pages/health/lecture/index'
      }
    ],
    medicineReminder: {
      time: '06:30',
      medicine: '卡托普利',
      dosage: '2片'
    }
  },

    onLoad: function() {
        this.checkAuth()
        this.getMedicineReminders()
    },

    onShow: function() {
        if (typeof this.getTabBar === 'function' &&
            this.getTabBar()) {
            this.getTabBar().setData({
                selected: 0
            })
        }
    },

    // 检查授权
    checkAuth: function() {
        wx.getSetting({
            success: (res) => {
                if (!res.authSetting['scope.userLocation']) {
                    wx.authorize({
                        scope: 'scope.userLocation',
                        success: () => {
                            this.getLocation()
                        },
                        fail: () => {
                            wx.showToast({
                                title: '需要位置权限才能为您提供更好的服务',
                                icon: 'none'
                            })
                        }
                    })
                } else {
                    this.getLocation()
                }
            }
        })
    },

    // 获取位置
    getLocation: function() {
        wx.getLocation({
            type: 'gcj02',
            success: (res) => {
                this.reverseGeocoding(res.latitude, res.longitude)
            }
        })
    },

    // 反向地理编码
    reverseGeocoding: function(latitude, longitude) {
        // 这里需要调用地图API进行反向地理编码
        // 示例使用的是高德地图API
        wx.request({
            url: 'https://restapi.amap.com/v3/geocode/regeo',
            data: {
                key: 'YOUR_AMAP_KEY',
                location: `${longitude},${latitude}`,
                extensions: 'base'
            },
            success: (res) => {
                if (res.data.status === '1') {
                    this.setData({
                        location: res.data.regeocode.addressComponent.city || '上海'
                    })
                }
            }
        })
    },

    // 选择位置
    chooseLocation: function() {
        wx.chooseLocation({
            success: (res) => {
                this.setData({
                    location: res.name || res.address
                })
            }
        })
    },

    // 搜索输入
    onSearchInput: function(e) {
        this.setData({
            searchText: e.detail.value
        })
    },

    // 搜索确认
    onSearch: function(e) {
        const keyword = e.detail.value || this.data.searchText
        if (keyword.trim()) {
            wx.navigateTo({
                url: `/pages/search/index?keyword=${encodeURIComponent(keyword)}`
            })
        }
    },

    // 标签点击
    onTagClick: function(e) {
        const tag = e.currentTarget.dataset.tag
        wx.navigateTo({
            url: `/pages/disease/detail/index?id=${tag.id}`
        })
    },

    // 导航到服务页面
    navigateToService: function(e) {
        const service = e.currentTarget.dataset.service
        wx.navigateTo({
            url: service.path
        })
    },

    // 导航到功能页面
    navigateToFunction: function(e) {
        const func = e.currentTarget.dataset.function
        wx.navigateTo({
            url: func.path
        })
    },

    // 查看用药详情
    viewMedicineDetail: function() {
        wx.navigateTo({
            url: '/pages/medicine/reminder/index'
        })
    },

    // 查看更多疾病
    viewMoreDiseases: function() {
        wx.navigateTo({
            url: '/pages/disease/library/index'
        })
    },

    // 获取用药提醒
    getMedicineReminders: function() {
        // 这里应该调用后端API获取用药提醒
        // 示例数据
        this.setData({
            medicineReminder: {
                time: '06:30',
                medicine: '卡托普利',
                dosage: '2片'
            }
        })
    },

  // 切换标签页
  switchTab: function(e) {
    const tab = e.currentTarget.dataset.tab
    if (tab === this.data.currentTab) return

    const tabUrls = {
      home: '/pages/index/index',
      life: '/pages/catalog/catalog',
      message: '/pages/cart/cart',
      my: '/pages/ucenter/index/index'
    }

    // 使用navigateTo而不是switchTab
    wx.navigateTo({
      url: tabUrls[tab]
    })

    this.setData({
      currentTab: tab
    })
  },

    // 显示添加选项
    showAddOptions: function() {
        wx.showActionSheet({
            itemList: ['发布动态', '发起活动', '健康记录'],
            success: (res) => {
                const actions = [
                    '/pages/post/create/index',
                    '/pages/activity/create/index',
                    '/pages/health/record/index'
                ]
                wx.navigateTo({
                    url: actions[res.tapIndex]
                })
            }
        })
    },

    onShareAppMessage: function() {
        return {
            title: '智慧养老服务平台',
            path: '/pages/index/index'
        }
    },

    onPullDownRefresh: function() {
        // 刷新页面数据
        Promise.all([
            this.getMedicineReminders(),
            this.getLocation()
        ]).then(() => {
            wx.stopPullDownRefresh()
        })
    }
})
